package com.sjgtw.cloud.web.commandoUnits.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sjgtw.cloud.util.EasyExcelUtil;
import com.sjgtw.cloud.web.commandoUnits.domain.CommandoUnits;
import com.sjgtw.cloud.web.commandoUnits.domain.vo.CommandoUnitsImportVo;
import com.sjgtw.cloud.web.commandoUnits.service.ICommandoUnitsService;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.web.rescueTeam.domain.RescueTeam;
import com.sjgtw.cloud.web.rescueTeam.domain.vo.RescueTeamImportVo;
import com.sjgtw.cloud.web.waterDamage.domain.WaterDamage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 抢险突击队Controller
 * @author ruoyi
 * @date 2024-03-09
 */
@RestController
@Api(tags = "1.3.2、值班管理/抢险突击队")
@RequestMapping("/commandoUnits")
public class CommandoUnitsController extends BaseController
{
    @Autowired
    private ICommandoUnitsService commandoUnitsService;

    /**
     * 查询抢险突击队列表
     */
   /* @RequiresPermissions("system:抢险突击队:list")*/
    @GetMapping("/list")
    @ApiOperation("查询抢险突击队列表")
    public TableDataInfo list(CommandoUnits commandoUnits)
    {
        startPage();
        List<Map<String,Object>> list = commandoUnitsService.selectCommandoUnitsList(commandoUnits);
        return getDataTable(list);
    }

    /**
     * 新增抢险突击队
     */
    //@RequiresPermissions("system:抢险突击队:add")
    @PostMapping("/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody CommandoUnits commandoUnits)
    {
        return toAjax(commandoUnitsService.insertCommandoUnits(commandoUnits));
    }

    /**
     * 修改抢险突击队
     */
    //@RequiresPermissions("system:抢险突击队:edit")
    @PutMapping("/edit")
    @ApiOperation("编辑")
    public AjaxResult edit(@RequestBody CommandoUnits commandoUnits)
    {
        return toAjax(commandoUnitsService.updateCommandoUnits(commandoUnits));
    }

    /**
     * 删除抢险突击队
     */
    //@RequiresPermissions("system:抢险突击队:remove")
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(commandoUnitsService.deleteCommandoUnitsById(id));
    }

    /**
     * 获取抢险突击队详细信息
     */
    // @RequiresPermissions("system:抢险突击队:query")
    @GetMapping(value = "/getInfo/{id}")
    @ApiOperation("查询详情")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(commandoUnitsService.selectCommandoUnitsById(id));
    }

    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommandoUnits commandoUnits)
    {
        List<CommandoUnitsImportVo> list = commandoUnitsService.selectExportCommandoUnitsList(commandoUnits);

        EasyExcelUtil<CommandoUnitsImportVo> util = new EasyExcelUtil<>(CommandoUnitsImportVo.class);
        util.exportExcel(response, list, "抢险突击队", "抢险突击队", "commandoUnits.xlsx");
    }

    /**
     * 下载模版
     * @param response
     */
    @ApiOperation(value = "下载模版")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response)
    {
        EasyExcelUtil<CommandoUnitsImportVo> util = new EasyExcelUtil<>(CommandoUnitsImportVo.class);
        util.exportExcel(response, new ArrayList<>(), "抢险突击队", "抢险突击队模版", "commandoUnits.xlsx");
    }


    /**
     * 导入
     * @param file
     * @return
     */
    @ApiOperation(value = "导入")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        EasyExcelUtil<CommandoUnitsImportVo> util = new EasyExcelUtil<>(CommandoUnitsImportVo.class);
        List<CommandoUnitsImportVo> entities = util.readExcel(file, 0, 2);
        if (entities == null || entities.size() == 0) {
            return error("没有读取到数据");
        }

        List<String> errors = commandoUnitsService.check(entities);
        if (errors.size() > 0) {
            return AjaxResult.error("", errors);
        }

        commandoUnitsService.batchSave(entities);
        return success();
    }



}
